<?php
class ProjectManageAction extends Action {
	protected function changestate($state){
		$Pro=D("Project");
		$condition ['pid'] = session ( 'pid' );
		$vo=$Pro->where($condition)->find();	
		$vo["state"]=$state;
	
		if ($Pro->save ( $vo )) {
			session ( "state", $state );
		} else
			$this->error ( "更新发错错误" );
		
	}
	public function select() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$Dao = D ( "ProjectView" );
		$list = $Dao->where ( "pid=" . $_GET ['pid'] )->find ();
		//echo "应该显示pid 为 " . $_GET ['pid'] . "的页面";
		//echo "应该显示pname 为 " . $_GET ['pname'] . "的页面";
		//echo $list ['pname'];
		session ( "pid", $list ['pid'] );
		session ( "cid", $list ['cid'] );
		session ( "gid", $list ['gid'] );
		session ( "pname", $list ['pname'] );
		session ( "pno", $list ['pno'] );
		session ( "state", $list ['state'] );
		session ( "managername", $list ['managername'] );
		session ( "partnername", $list ['partnername'] );
		session ( "fund", $list ['fund'] );
		$this->display ();
	}
	public function checkProjName() {
		$q = strtolower ( $_GET ["term"] );
		if (! $q)
			return;
		$Dao = M ( "Project" );
		
		$items = $Dao->getField ( "pid,pname" );
		
		$result = array ();
		foreach ( $items as $id => $key ) {
			if (strpos ( strtolower ( $key ), $q ) !== false) {
				array_push ( $result, array ("value" => $key ) );
			}
			if (count ( $result ) > 11)
				break;
		}
		echo json_encode ( $result );
	}
	public function checkFundName() {
		$q = strtolower ( $_GET ["term"] );
		if (! $q)
			return;
		$Dao = M ( "Project" );
		
		$items = $Dao->getField ( "pid,fund" );
		
		$result = array ();
		foreach ( $items as $id => $key ) {
			if (strpos ( strtolower ( $key ), $q ) !== false) {
				array_push ( $result, array ("value" => $key ) );
			}
			if (count ( $result ) > 11)
				break;
		}
		echo json_encode ( $result );
	}
	public function checkName() { //
		$Dao = M ( "User" );
		$condition ['username'] = $_POST ['username'];
		// 查询数据
		$list = $Dao->where ( $condition )->select ();
		echo 'list';
		if ($list) {
			$this->success ( $list ['username'] );
		} else {
			$this->ajaxReturn ( 0, "新增成功！", 1 );
		}
	}
	public function show() {
		if (session ( 'uid' )) {
			import ( "@.ORG.Page" );
			$Pname = $_GET ["pname"];
			$Managername = $_GET ["managername"];
			$Fund = $_GET ["fund"];
			
			$Dao = D ( "ProjectView" );
			
			$condition ['pname'] = array ('like', '%' . $Pname . '%' );
			$condition ['managername'] = array ('like', '%' . $Managername . '%' );
			$condition ['fund'] = array ('like', '%' . $Fund . '%' );
			
			$count = $Dao->where ( $condition )->count ();
			
			$p = new Page ( $count, 10 );
			foreach ( $condition as $key => $val ) {
				$p->parameter .= "$key=" . urlencode ( $val ) . "&";
			}
			// 分页显示输出
			$page = $p->show ();
			
			$list = $Dao->where ( $condition )->order ( 'pname ASC' )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
			
			$this->assign ( 'page', $page );
			$this->assign ( 'list', $list );
			$this->display ();
		
		} else
			$this->error ( "尚未登录" );
	
	}
	public function file() {
		//session ( 'project', '殷三三' );
		//session ( 'level', '投后管理' );
		$file = D ( "file" );
		//在这里应该从session中读取当前的project名称，然后从数据库中读取
		//$project=$_SESSION['project'];	
		//为了测试方便，这里只用project2
		$project = session ( 'pname' );
		
		$level=$_GET ["level"];
		
		if ($level === '初访') {
			$filelist = array ("商业计划书", "可行性报告", "尽职调查报告", "初访备忘录", "行业分析报告", "财务报表", "保密协议" );
		} elseif ($level === '立项') {
			$filelist = array ("项目立项申请表", "立项决议", "项目立项报告及附件", "立项公司财务模型" );
		} elseif ($level === '深入DD') {
			$filelist = array ("法律、财务等尽职报告", "TermSheet", "商业实质尽调", "项目公司关键人物的尽调", "项目尽职调查清单", "项目财务审计报告" );
		} elseif ($level === '投决') {
			$filelist = array ("项目投决申请表", "投资决策委员会决议", "项目投资建议书/报告及附件", "项目投资协议", "股票证", "变更后的股东名单", "公司章程", "验资证明/托管银行流程凭证", "股东协议" );
		} elseif ($level === '投后管理') {
			$filelist = array ("增值服务情况说明", "项目投后访谈及重大事项备忘录", "项目投后管理季度报告", "公司季度财务报表", "公司年报", "公司董事会决议", "公司股东会决议", "公司工商变更最新材料：营业执照、公司章程等", "各轮融资的验资报告" );
		} elseif ($level === '退出') {
			$filelist = array ("公司退出协议" );
		}
		
		$condition ['project'] = $project;
		$condition ['level'] = $level;
		$list = $file->where ( $condition )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'filelist', $filelist );
		$this->assign ( 'level', $level );
		$this->assign ( 'project', $_SESSION ['pname'] );
		$this->display ();
	}
	public function fileall() {
		//session ( 'project', '殷三三' );
		//session ( 'level', '投后管理' );
		$file = D ( "file" );
		//在这里应该从session中读取当前的project名称，然后从数据库中读取
		//$project=$_SESSION['project'];	
		//为了测试方便，这里只用project2
		$project = session ( 'pname' );
			
		$condition ['project'] = $project;
		$list = $file->where ( $condition )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'filelist', $filelist );
		$this->assign ( 'project', $_SESSION ['pname'] );
		$this->display ();
	}
	
	public function delete() {
		header ( "Content-Type:text/html; charset=utf-8" );
		
		$condition ['orginalname'] = $_GET ['name'];
		//需要删除真正的目录文件
		$Del = M ( "File" );
		$vo = $Del->where ( $condition )->find ();
		//echo getcwd();
		//$path="/Public/example/uploads/".$vo['project']."/".$vo['orginalname'];
		//$path=$vo['path'];
		

		//echo $path;
		

		/*	if (file_exists ( $path )) {
			echo '文件存在,可以删了';
			unlink($path);
		} else {
			echo '文件不存在,可能路径添错了';
		}*/
		
		$Dao = M ( "File" );
		
		$result = $Dao->where ( $condition )->delete ();
		
		if ($result !== false) {
			$this->success ( '删除成功' );
		} else {
			$this->error ( '删除失败' );
		}
	
	}
	public function meetinglog() {
		import ( "@.ORG.Page" );
		$Dao = M ( 'Meetinglog' );
		$pid = session ( 'pid' );
		$condition ['project_pid'] = $pid; //测试用
		

		$count = $Dao->where ( $condition )->count ();
		
		$p = new Page ( $count, 10 );
		
		foreach ( $condition as $key => $val ) {
			$p->parameter .= "$key=" . urlencode ( $val ) . "&";
		}
		
		$p->setConfig ( 'header', '条记录' );
		$page1 = $p->show ();
		
		// 当前页数据查询
		

		$list = $Dao->where ( $condition )->order ( 'date ASC' )->limit ( $p->firstRow . ',' . $p->listRows )->select ();
		
		$this->assign ( 'page1', $page1 );
		$this->assign ( "mlist", $list );
		$this->display ();
	}
	public function addlog() {
		
		$Dao = D ( "Meetinglog" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	}
	public function deletelog() {
		
		$Dao = D ( "Meetinglog" );
		$condition ['idmeetinglog'] = $_GET ['mid'];
		
		$result = $Dao->where ( $condition )->delete ();
		
		if ($result !== false) {
			$this->success ( "删除记录成功" );
		} else {
			$this->error ( "删除记录失败" );
		}
	}
	public function addproject() {
		$ProjTB = D ( 'Project' );
		$CompanyTB = D ( 'Company' );
		$GroupTB = D ( 'Projectgroup' );
		
		$Proj ['pno'] = $_POST ['pno'];
		$Proj ['pname'] = $_POST ['pname'];
		$Proj ['state'] = '活跃';
		$Proj ['regdate'] = $_POST ['date'];
		
		$lastInsId = $ProjTB->add ( $Proj );
		
		$Company ['project_pid'] = $lastInsId;
		$Company ['name'] = $_POST ['cname'];
		$Company ['address'] = $_POST ['address'];
		$Company ['classify'] = $_POST ['classify'];
		$Company ['contact_name'] = $_POST ['cpname'];
		$Company ['contact_position'] = $_POST ['cpposition'];
		$Company ['contact_phone'] = $_POST ['cpphone'];
		$Company ['contact_email'] = $_POST ['cpemail'];
		$Company ['intro'] = $_POST ['desc'];
		
		$Group ['project_pid'] = $lastInsId;
		$Group ['source'] = $_POST ['source'];
		$Group ['partnername'] = $_POST ['partnername'];
		$Group ['managername'] = $_POST ['managername'];
		$Group ['membername'] = $_POST ['membername'];
		$Group ['prefund'] = $_POST ['prefund'];
		
		if ($CompanyTB->add ( $Company ) && $GroupTB->add ( $Group )) {
			$this->success ( "新建成功" );
		} else
			$this->error ( "新建发错错误" );
	}
	public function updateProject() {
		
		$ProjTB = D ( 'Project' );
		$CompanyTB = D ( 'Company' );
		$GroupTB = D ( 'Projectgroup' );
		
		$Proj ['pid'] = session ( 'pid' );
		$Proj ['pno'] = $_POST ['pno'];
		$Proj ['pname'] = $_POST ['pname'];
		$Proj ['regdate'] = $_POST ['date'];
		$Proj ['state'] = $_POST ['state'];
		session ( "state", $_POST ['state']);
		
		$Company ['cid'] = session ( 'cid' );
		$Company ['project_pid'] = session ( 'pid' );
		$Company ['name'] = $_POST ['cname'];
		$Company ['address'] = $_POST ['address'];
		$Company ['classify'] = $_POST ['classify'];
		$Company ['contact_name'] = $_POST ['cpname'];
		$Company ['contact_position'] = $_POST ['cpposition'];
		$Company ['contact_phone'] = $_POST ['cpphone'];
		$Company ['contact_email'] = $_POST ['cpemail'];
		$Company ['intro'] = $_POST ['desc'];
		
		$Group ['gid'] = session ( 'gid' );
		$Group ['project_pid'] = session ( 'pid' );
		$Group ['source'] = $_POST ['source'];
		$Group ['partnername'] = $_POST ['partnername'];
		$Group ['managername'] = $_POST ['managername'];
		$Group ['membername'] = $_POST ['membername'];
		$Group ['prefund'] = $_POST ['prefund'];
		
		if (false !== $ProjTB->save ( $Proj ) && false !== $GroupTB->save ( $Group ) && false !== $CompanyTB->save ( $Company )) {
			$this->success ( "更新成功" );
		} else
			$this->error ( "更新发错错误" );
	}
	public function info() {
		$Dao = D ( 'ProjectView' );
		$condition ['pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->find ();
		$this->assign ( 'project', $list );
		
		$this->display ();
	}
	public function preinvest_1() {
		$Dao = D ( 'Infofinance' );
		$condition ['project_pid'] = session ( 'pid' );
		$last = $Dao->where ( $condition )->order ( "idinfo_finance DESC " )->limit ( "0,1" )->find ();
		$this->assign ( "last", $last );
		$this->display ();
	
	}
	public function setup() {
		header ( "Content-Type:text/html; charset=utf-8" );
		echo '立项' . $_SESSION ['pname'];
	}
	public function DD() {
		header ( "Content-Type:text/html; charset=utf-8" );
		echo '深入DD' . $_SESSION ['pname'];
	}
	public function determine() {
		header ( "Content-Type:text/html; charset=utf-8" );
		echo '投决' . $_SESSION ['pname'];
	}
	public function postinvest() {
		header ( "Content-Type:text/html; charset=utf-8" );
		echo '投后' . $_SESSION ['pname'];
	}
	public function postinvest_4() {
		$Dao = D ( 'company_finance' );
		$condition ['project_pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->order ( "date DESC " )->select ();
		$this->assign ( "list", $list );
		$this->display ();
	
	}
	public function postinvest_5() {
		$Dao = D ( 'info_tracking' );
		$condition ['project_pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->order ( "date DESC " )->select ();
		$this->assign ( "list", $list );
		$this->display ();
	}
	public function postinvest_6() {
		$Dao = D ( 'additionservice' );
		$condition ['project_pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->order ( "date DESC " )->select ();
		$this->assign ( "list", $list );
		$this->display ();
	}
	public function postinvest_7() {
		$Dao = D ( 'examine' );
		$condition ['project_pid'] = session ( 'pid' );
		$condition ['type'] = "项目参与人及贡献权重";
		$list = $Dao->where ( $condition )->select ();
		$this->assign ( "list1", $list );
		
		$condition ['type'] = "投后管理人员的贡献";
		$list = $Dao->where ( $condition )->select ();
		
		$this->assign ( "list2", $list );
		$this->display ();
	}
	
	public function addPreinvest() {
		$Dao = D ( 'Infofinance' );
		$vo = $Dao->create ();
		$vo ['project_pid'] = session ( 'pid' );
		if ($result = $Dao->add ( $vo ))
			$this->success ( "成功" );
		else
			$this->error ( "失败" );
	
	}
	
	public function postinvest_1() {
		//session('pid','2');
		$investchage = D ( 'investchange' );
		$pid = $_SESSION ['pid'];
		$condition ['pid'] = $pid;
		//获取第一次投资
		$condition ['investnum'] = '1';
		$list1 = $investchage->where ( $condition )->select ();
		//获取第二次投资
		$condition ['investnum'] = '2';
		$list2 = $investchage->where ( $condition )->select ();
		//获取第三次投资
		$condition ['investnum'] = '3';
		$list3 = $investchage->where ( $condition )->select ();
		
		$this->assign ( 'list1', $list1 );
		$this->assign ( 'list2', $list2 );
		$this->assign ( 'list3', $list3 );
		
		$this->display ();
	}
/*	public function postinvest_1_5() {
		$Dao = D ( 'assessment' );
		$condition ['project_pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->order ( "date DESC " )->select ();
		$this->assign ( "list", $list );
		$this->display ();	
	}*/
	public function postinvest_2() {
		$Dao = D ( 'safeguardclause' );
		$condition ['project_pid'] = $_SESSION ['pid'];
		$list = $Dao->where ( $condition )->find ();
		session ( "sid", $list ["idsafeguardclause"] );
		$this->assign ( 'clause', $list );
		$this->display ();
	}
	
	public function valuechange() {
		//session('pid','2');
		$Mysql = D ( 'valuechange' );
		$vo = $Mysql->create ();
		$vo [pid] = session ( 'pid' );
		if ($result = $Mysql->add ( $vo )) {
			$this->success ( "成功" );
		} else
			$this->error ( "失败" );
	}
	
	public function investchange() {
		//session('pid','2');
		$Mysql = D ( 'investchange' );
		$vo = $Mysql->create ();
		$vo [pid] = session ( 'pid' );
		if ($result = $Mysql->add ( $vo )) {
			$this->success ( "成功" );
		} else
			$this->error ( "失败" );
	}
	public function postinvest_4_add() {
		
		$Dao = D ( "company_finance" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	
	}
	public function postinvest_5_add() {
		
		$Dao = D ( "info_tracking" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	
	}
	public function postinvest_6_add() {
		
		$Dao = D ( "additionservice" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	
	}
	
	public function postinvest_7_add() {
		
		$Dao = D ( "examine" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	
	}
	public function postinvest_2_update() {
		if (session ( 'sid' )) {
			$Dao = D ( "safeguardclause" );
			$vo ["idsafeguardclause"] = session ( 'sid' );
			$vo ['project_pid'] = session ( 'pid' );
			if ($_POST ['type'] == "其他")
				$vo ['type'] = $_POST ['other'];
			else
				$vo ['type'] = $_POST ['type'];
			
			$vo ['condition'] = $_POST ['condition'];
			$vo ['content'] = $_POST ['content'];
			
			if (false !== $Dao->save ( $vo )) {
				$this->success ( "更新成功" );
			} else
				$this->error ( "更新发错错误" );
		
		} else {
			
			$Dao = D ( "safeguardclause" );
			
			$vo ['project_pid'] = session ( 'pid' );
			if ($_POST ['type'] == "其他")
				$vo ['type'] = $_POST ['other'];
			else
				$vo ['type'] = $_POST ['type'];
			
			$vo ['condition'] = $_POST ['condition'];
			$vo ['content'] = $_POST ['content'];
			if ($Dao->add ( $vo )) {
				$this->success ( "添加成功" );
			} else
				$this->error ( "添加错误" );
		}
	}

	public function quitinfo(){
		$Dao = D ( 'quitinfo' );
		$condition ['project_pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->order ( "date DESC " )->select ();
		$this->assign ( "list", $list );
		$this->display ();
	
	}
	public function quitinfo_add(){
		 $this->changestate("退出");
		
		$Dao = D ( "quitinfo" );
		if ($vo = $Dao->create ()) {
			$vo ['project_pid'] = session ( 'pid' );
			if (false !== $Dao->add ( $vo )) {
				$this->ajaxReturn ( $vo, '记录添加成功！', 1 );
			} else {
				$this->error ( '记录添加失败！' );
			}
		} else {
			$this->error ( $Dao->getError () );
		}
	}
	public function export(){
		$Dao = D ( 'AllView' );
		$condition ['pid'] = session ( 'pid' );
		$list = $Dao->where ( $condition )->find();
		$this->assign ( 'project', $list );
		$this->display ();
	}
	
	
	
}